<?php
namespace App\Models;

class Area extends CommonModel {
	protected $table = 'area';
	
	/**
	 * @param int $parent_id
	 * @return mixed
	 * 只显示一层，没有子集
	 */
	public static function children($parent_id = 0){
		$data = static::select(['id','parent_id','name','name as text'])->where(['parent_id'=>$parent_id])->get();
		return $data;
	}
	
	
	/**
	 * @param int $parent_id
	 * @return array
	 * 只有显示型菜单
	 */
	public static function treeChildren($parent_id = 0){
		$cache_key = 'area_child_4'.$parent_id;
		$data = cache_callback($cache_key,function() use ($parent_id){
			$data = static::select(['id','parent_id','name','name as text'])->where(['parent_id'=>$parent_id])->get()->each(function($v,$k){
				$v->children = static::treeChildren($v->id);
			});
			return $data;
		});
		return $data;
	}
}